package com.google.code.datastructure;

import java.util.ArrayList;

public class DataTable {
	private ArrayList<DataColumn> columns;
	private ArrayList<DataRow> rows;

	// getter & setter
	public ArrayList<DataColumn> getColumns() {
		return this.columns;
	}

	public ArrayList<DataRow> getRows() {
		return this.rows;
	}

	// constructor
	public DataTable() {
		this.columns = new ArrayList<DataColumn>();
		this.rows = new ArrayList<DataRow>();
	}

	// methods
	public void addColumn(DataColumn col) throws Exception {
		if (columnNameExists(col.getColumnName()))
			throw new Exception("Already has a column named: "
					+ col.getColumnName());
		
		this.columns.add(col);
	}

	public void addRow(DataRow row) {
		this.rows.add(row);
	}

	public DataRow newRow() {
		return new DataRow(this);
	}

	// private methods
	private boolean columnNameExists(String columnName) {
		if (columnName == null)
			return false;

		for (DataColumn dc : this.columns) {
			if (dc.getColumnName() == null)
				continue;
			if (dc.getColumnName().equals(columnName))
				return true;
		}

		return false;
	}
}
